package com.hyh.shiyanshiguanli.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hyh.shiyanshiguanli.constant.MessageConstant;
import com.hyh.shiyanshiguanli.context.BaseContext;
import com.hyh.shiyanshiguanli.domain.dto.PublicInformationAddDTO;
import com.hyh.shiyanshiguanli.domain.dto.PublicInformationDTO;
import com.hyh.shiyanshiguanli.domain.dto.PublicInformationUpdateDTO;
import com.hyh.shiyanshiguanli.domain.entity.Admin;
import com.hyh.shiyanshiguanli.domain.entity.PublicInformation;
import com.hyh.shiyanshiguanli.domain.result.PageResult;
import com.hyh.shiyanshiguanli.mapper.AdminMapper;
import com.hyh.shiyanshiguanli.mapper.PublicInformationMapper;
import com.hyh.shiyanshiguanli.service.PublicInformationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @author hyh
 * @version 1.0
 */
@SuppressWarnings({"ALL"})
@Service
public class PublicInformationImpl implements PublicInformationService {


    @Autowired
    PublicInformationMapper publicInformationMapper;

    @Autowired
    AdminMapper adminMapper;


    /**
     * 公告的分页查询
     * @param publicInformationDTO
     * @return
     */
    @Override
    public PageResult page(PublicInformationDTO publicInformationDTO) {
        PageHelper.startPage(publicInformationDTO.getPage(),publicInformationDTO.getPageSize());
        Page<PublicInformation> publicInformations = publicInformationMapper.page(publicInformationDTO);
        long total = publicInformations.getTotal();
        List<PublicInformation> result = publicInformations.getResult();

        PageResult result1 = PageResult.builder()
                .recodes(result)
                .total(total)
                .build();
        return result1;
    }

    /**
     * 根据id查询
     * @param id
     * @return
     */
    @Override
    public PublicInformation selectById(Integer id) {
        PublicInformation publicInformation = publicInformationMapper.selectById(id);
        return publicInformation;

    }

    /**
     * 根据id更新
     * @param publicInformationUpdateDTO
     */
    @Override
    public void updateById(PublicInformationUpdateDTO publicInformationUpdateDTO) {
        publicInformationMapper.updateById(publicInformationUpdateDTO);
    }

    /**
     * 添加
     * @param publicInformationAddDTO
     */
    @Override
    public void add(PublicInformationAddDTO publicInformationAddDTO) {
        PublicInformation publicInformation = new PublicInformation();
        BeanUtils.copyProperties(publicInformationAddDTO,publicInformation);
        publicInformation.setCreateTime(LocalDateTime.now());
        Admin admin = adminMapper.selectById(BaseContext.getCurrentId().intValue());
        publicInformation.setCreatePeople(admin.getUsername());
        publicInformationMapper.add(publicInformation);

    }

    @Override
    public int deleteById(Integer id) {
        publicInformationMapper.deleteById(id);
        return MessageConstant.SUCCESS;
    }
}
